package com.cjh.crm.service.Impl;

import com.cjh.crm.bean.Customer;
import com.cjh.crm.mapper.CustomerMapper;
import com.cjh.crm.model.PageResult;
import com.cjh.crm.model.ReportsResult;
import com.cjh.crm.service.ICustomerService;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ICustomerServiceImpl implements ICustomerService {

    @Autowired(required = false)
    CustomerMapper customerMapper;

    /*
    分页查询所有客户
     */
    @Override
    public PageResult<Customer> findPageResult(Customer customer, int page, int limit) {

        PageResult<Customer> pr = new PageResult<>();
        pr.setCode(0);
        pr.setMsg("");


        Map<String, Object> map = new HashMap<>();
        if (String.valueOf(customer.getCusNo()) == "") {
            customer.setCusNo(null);
        }
        if (customer.getCusName() == "") {
            customer.setCusName(null);
        }
        map.put("start", (page - 1) * limit);
        map.put("limit", limit);
        map.put("customer", customer);

        int count = customerMapper.findCount(map);
        pr.setCount(count);

        List<Customer> customers = customerMapper.findCustomer(map);
        pr.setData(customers);
        System.out.println(customers.toString());
        return pr;
    }

    @Override
    public void save(Customer customer) {
        customerMapper.save(customer);
    }

    @Override
    public Customer findCustomerById(int cusNo) {
        Customer customer = customerMapper.findCustomerById(cusNo);
        return customer;
    }

    @Override
    public boolean delCustomer(Integer cusNo) {

        return customerMapper.delCustomer(cusNo);
    }

    @Override
    public boolean costomerUpdate(Customer customer) {
//        System.out.println("打印："+customer);
        return customerMapper.costomerUpdate(customer);
    }

    @Override
    public List<ReportsResult> toCountCustomer() {
        return customerMapper.toCountCustomer();
    }


}
